import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export const useRegistrationStore = defineStore('registration', () => {
  const registrations = ref([])
  const loading = ref(false)
  const error = ref(null)
  const stats = ref({
    appointments: 0,
    completed: 0
  })

  // 获取所有挂号记录
  const fetchRegistrations = async () => {
    try {
      loading.value = true
      const response = await axios.get('http://localhost:8080/api/registrations')
      registrations.value = response.data
      updateStats()
    } catch (err) {
      error.value = err.response?.data?.message || '获取挂号记录失败'
      console.error('Error fetching registrations:', err)
    } finally {
      loading.value = false
    }
  }

  // 获取单个挂号记录
  const fetchRegistrationById = async (id) => {
    try {
      loading.value = true
      const response = await axios.get(`http://localhost:8080/api/registrations/${id}`)
      return response.data
    } catch (err) {
      error.value = err.response?.data?.message || '获取挂号记录详情失败'
      console.error(`Error fetching registration ${id}:`, err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 添加挂号记录
  const addRegistration = async (registrationData) => {
    try {
      loading.value = true
      const response = await axios.post('http://localhost:8080/api/registrations', registrationData)
      registrations.value.push(response.data)
      updateStats()
      return response.data
    } catch (err) {
      error.value = err.response?.data?.message || '添加挂号记录失败'
      console.error('Error adding registration:', err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 更新挂号记录
  const updateRegistration = async (id, registrationData) => {
    try {
      loading.value = true
      const response = await axios.put(`http://localhost:8080/api/registrations/${id}`, registrationData)
      const index = registrations.value.findIndex(r => r.id === id)
      if (index !== -1) {
        registrations.value[index] = response.data
      }
      updateStats()
      return response.data
    } catch (err) {
      error.value = err.response?.data?.message || '更新挂号记录失败'
      console.error(`Error updating registration ${id}:`, err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 删除挂号记录
  const deleteRegistration = async (id) => {
    try {
      loading.value = true
      await axios.delete(`http://localhost:8080/api/registrations/${id}`)
      registrations.value = registrations.value.filter(r => r.id !== id)
      updateStats()
    } catch (err) {
      error.value = err.response?.data?.message || '删除挂号记录失败'
      console.error(`Error deleting registration ${id}:`, err)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 更新统计信息
  const updateStats = () => {
    stats.value.appointments = registrations.value.length
    stats.value.completed = registrations.value.filter(r => r.status === '已完成').length
  }

  return {
    registrations,
    stats,
    loading,
    error,
    fetchRegistrations,
    fetchRegistrationById,
    addRegistration,
    updateRegistration,
    deleteRegistration
  }
})